package com.eversec.util.command;

import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * 压缩类
 * @author 潘保涛
 *
 */
public class Command {
	
//	private static Logger log = Logger.getLogger(Command.class);
	
	/**
	 * zip压缩
	 * @param compressType 压缩类型，0：无压缩，1：zip压缩
	 * @param commandString
	 * @param home 存储zip文件的根路径
	 * @param path
	 * @return
	 */
	public byte[] zipCompression(String compressType, String commandString, String home, String subPath, String path){
		byte[] zipxml = null;
		if(commandString != null){
			if (compressType != null && compressType.equals("0")) { // 无压缩
				try {
					zipxml = commandString.getBytes(Constant.CHARACTER_SET);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else { // zip压缩
				if (!home.endsWith(File.separator)) {
					home = home + File.separator;
		        }
				home = home + subPath;
				if (!home.endsWith(File.separator)) {
					home = home + File.separator;
		        }
				home = home + path;
				zipxml = new ZipUtil().zipToBytes(home, commandString);
			}
		}
		
		return zipxml;
	}
	
	/**
	 * zip解压缩
	 * @param compressType
	 * @param dataArray
	 * @return
	 * @throws Exception 
	 */
	public String unZipCompression(String compressType, byte[] dataArray) throws Exception{
		String xmlData = null;
		if (compressType != null && compressType.equals("0")) { // 无压缩
			try {
				xmlData = new String(dataArray, Constant.CHARACTER_SET);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		} else { // zip解压缩
				xmlData = new ZipUtil().unzipToString(dataArray);
		}
		return xmlData;
	}
	
	/**
	 * 哈希加密
	 * @param hashType 加密类型
	 * @param hashTypeCount MD5时加密长度
	 * @param zipxml 指令文件压缩后的结果
	 * @param messageKey 消息认证钥匙
	 * @return
	 */
	public String hashAlgorithm(String hashType, String hashTypeCount, byte[] zipxml, String messageKey){
		//将messageKey转换成byte[]和zipxml相加后转换成字符串--->[x,y,z]
		byte[] MD5xml = ByteArrayUtil.bytesAddStr(zipxml, messageKey, Constant.CHARACTER_SET);
		String commandhash = "";
		if (hashType != null && hashType.equals("0")) { // 无hash
			commandhash = StringUtil.getBaseByByte(MD5xml);
		} else if (hashType != null && hashType.equals("1")) { // MD5加密
			if ("1".equals(hashTypeCount)) {
				String MD5xmlStr = StringUtil.getMD5(MD5xml).substring(8, 24);
				commandhash = StringUtil.getBaseByString(MD5xmlStr);
			} else {
				String MD5xmlStr = StringUtil.getMD5(MD5xml);
				System.out.println("------------MD5加密后的字符串: " + MD5xmlStr);
				commandhash = StringUtil.getBaseByString(MD5xmlStr);
				System.out.println("------------BASE64加密后的字符串: " + commandhash);
			}
		} else if(hashType != null && hashType.equals("2")){ // SHA-1加密
			String MD5xmlStr = StringUtil.SHAEncrypt(MD5xml);
			System.out.println("------------SHA-1加密后的字符串: " + MD5xmlStr);
			commandhash = StringUtil.getBaseByString(MD5xmlStr);
			System.out.println("------------BASE64加密后的字符串: " + commandhash);
		}
		return commandhash;
	}
	
	/**
	 * 对称加密
	 * @param encryptType 加密类型
	 * @param zipxml 指令文件压缩后的结果
	 * @param aesKey AES加密钥匙
	 * @param aesOffsets AES加密偏移量
	 * @return
	 */
	public byte[] encryptAlgorithm(String encryptType, byte[] zipxml, String aesKey, String aesOffsets){
		byte[] command = null;
		
		if (encryptType != null && encryptType.equals("0")) { // 不加密
			command = zipxml;
		} else { // AES加密算法
			command = StringUtil.aes(zipxml, aesKey, aesOffsets);
		}
		return command;
	}
	
	/**
	 * 对称解密
	 * @param encryptType 加密类型
	 * @param zipxml 指令文件压缩后的结果
	 * @param aesKey AES加密钥匙
	 * @param aesOffsets AES加密偏移量
	 * @return
	 */
	public byte[] decryptAlgorithm(String encryptType, byte[] basexml, String aesKey, String aesOffsets) throws Exception{
		byte[] command = null;
		
		if (encryptType != null && encryptType.equals("0")) { // 不加密
			command = basexml;
		} else { // AES加密算法
			command = StringUtil.decrypt(basexml, aesKey, aesOffsets);
		}
		return command;
	}
	
	/**
	 * 身份认证
	 * @param userKey
	 * @param randomStr
	 * @param hashType
	 * @param hashTypeCount
	 * @return
	 */
	public String pwdHash(String userKey, String randomStr, String hashType, String hashTypeCount){
		String pwdHash = userKey + randomStr;
		if (hashType != null && hashType.equals("0")) { // 无hash

		} else if (hashType != null && hashType.equals("1")) { // MD5
			if("1".equals(hashTypeCount)){
				pwdHash = StringUtil.getMD5(pwdHash.getBytes()).substring(8, 24);
			}else{
				pwdHash = StringUtil.getMD5(pwdHash.getBytes());
			}
		} else if(hashType != null && hashType.equals("2")){ // SHA-1加密
			pwdHash = StringUtil.SHAEncrypt(pwdHash.getBytes());
		}
		
		return pwdHash;
	}
}
